package slap.util;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;


public class Xml {
	
	public static Document openXml(File xmlFile) throws DocumentException{
		SAXReader saxReader = new SAXReader();
		return saxReader.read(xmlFile);
	}
	
	public static String getText(Document document, String xpath){
		return getText(document.selectNodes(xpath).get(0));
	}
	
	public static String getText(Node node, String xpath){
		return getText(node.selectNodes(xpath).get(0));
	}
	
	static Pattern TRIM = Pattern.compile("^[\\n|\\s]*(.*)[\\n|\\s]*$", Pattern.MULTILINE);
	public static String getText(Node node){
		String text =  node.getText();
		Matcher mat = TRIM.matcher(text);
		if(mat.matches()){
			text = mat.group(1);
		}
		return text;
	}
	
}
